# coding: utf-8

# Copyright (c) P SOFTHOUSE. All rights reserved.

import os

from PencilLineGuiUtils import loadUiType, sortStrings
from PSOFT.vendor.Qt_py.Qt import QtCore, QtGui, QtWidgets

CURRENT_PATH = os.path.dirname(__file__)

# ----------------------------------------------------------------------------
# GUIを作るクラス
class GUI(QtWidgets.QDialog):

    # コンストラクタ
    def __init__(self, uiLabels, defaultCheckState,
                 itemsChecked, itemsNoCheck):

        super(GUI, self).__init__()

        # Load our dialog class from its .ui definition file.
        addItemsDialog_ui = os.path.join(CURRENT_PATH, 'PencilAddItemsDialog.ui')
        Ui_addItemsDialog = loadUiType(addItemsDialog_ui)[0]

        self.ui = Ui_addItemsDialog()
        self.ui.setupUi(self)
        self.setWindowTitle(uiLabels['title'])

        # リストビューのモデル
        self.__addListModel = QtCore.QStringListModel()

        self._itemsChecked = sortStrings(itemsChecked)
        self._itemsNoCheck = sortStrings(itemsNoCheck)

        # lineListViewへのモデルの登録
        self.ui.addListView.setModel(self.__addListModel)

        self.ui.addItemsButtonAdd.pressed.connect(self.onOKButton)
        self.ui.addItemsButtonAdd.setText(uiLabels['buttonadd'])

        self.ui.addItemsButtonClose.setText(uiLabels['buttoncancel'])

        if defaultCheckState:
            self.__addListModel.setStringList(self._itemsChecked)
        else:
            self.__addListModel.setStringList(self._itemsNoCheck)

        self.ui.addItemsCheckBox.setText(uiLabels['checkbox'])
        self.ui.addItemsCheckBox.stateChanged.connect(self.checkBoxStateChanged)

        self.accepted.connect(self.onOKButton)

        self.selection = []

    def checkBoxStateChanged(self):
        if (self.ui.addItemsCheckBox.isChecked()):
            self.__addListModel.setStringList(self._itemsChecked)
        else:
            self.__addListModel.setStringList(self._itemsNoCheck)

    def onOKButton(self):
        self.selection = []
        for i in self.ui.addListView.selectionModel().selectedIndexes():
            self.selection.append(i.data())
        self.close()


# ------------------------------------------------------------------------------
# GUIの起動
def showWindow(uiLabels, defaultCheckState,
               itemsChecked, itemsNoCheck):
    ui = GUI(uiLabels, defaultCheckState,
             itemsChecked, itemsNoCheck)
    # The dialog will be modal
    ui.exec_()

    return ui.selection
